//
//  ZYMainViewController.swift
//  ZYWeibo
//
//  Created by Mac on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZYMainViewController: UITabBarController {

    //按钮的点击回调
    func composeClick(){
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        ///添加4个子控制器
        addViewControllers()
        
//        self.tabBar = ZYTabBar()    //不能直接赋值，他是一个只读属性，要用kvc来赋值
        ///创建自定义的tabBar，并为中间的按钮添加事件
        let newTabbar = ZYTabBar()
        setValue(newTabbar, forKey: "tabBar")  //设置自定义的tabBar
        
        newTabbar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)//添加按钮点击
    }
    
    ///添加4个子控制器的方法
    func addViewControllers(){
        //创建首页导航控制器
        let homeNav = UINavigationController(rootViewController: ZYHomeTableViewController())
        homeNav.title = "首页"
        homeNav.tabBarItem.image = UIImage(named: "tabbar_home")
        
        //        homeNav.tabBarItem.selectedImage = UIImage(named: "tabbar_home_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//设置图片按原始图片的样式显示
        //        homeNav.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Normal)
        
        //创建消息导航控制器
        let messageNav = UINavigationController(rootViewController: ZYMessageTableViewController())
        messageNav.title = "消息"
        messageNav.tabBarItem.image = UIImage(named: "tabbar_message_center")
        
        //创建中间导航控制器
//        let addNav = UINavigationController(rootViewController: ZYDiscoverTableViewController())
//        addNav.title = ""
//        addNav.tabBarItem.image = UIImage(named: "ee")
        
        //创建发现导航控制器
        let discoverNav = UINavigationController(rootViewController: ZYDiscoverTableViewController())
        discoverNav.title = "发现"
        discoverNav.tabBarItem.image = UIImage(named: "tabbar_discover")
        
        //创建我导航控制器
        let profileNav = UINavigationController(rootViewController: ZYProfileTableViewController())
        profileNav.title = "我"
        profileNav.tabBarItem.image = UIImage(named: "tabbar_profile")
        
        //添加导航控制器控制器
        viewControllers = [homeNav,messageNav,discoverNav,profileNav]
    }
    
    ///懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        // 按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 按钮的背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
}
